namespace Profit.ViewModels
{
    using System;
    using System.Windows;
    using System.Windows.Input;
    using Microsoft.Win32;
    using Views.Interfaces;

    public sealed class PortraitViewModel : ViewModelBase<IPortraitView>
    {
        private ICommand _browseCommand = null;
        private ICommand _clearCommand = null;

        private string _imagePath;

        private const string EMPTY_IMAGE_PATH = "/Profit;component/Resources/Icons/agent_default_64.png";

        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="view">The IView instance associated</param>
        /// <param name="imagePath"></param>
        public PortraitViewModel(IPortraitView view, string imagePath) 
            : base(view)
        {
            _imagePath = imagePath;
        }

        public ICommand BrowseCommand
        {
            get{return _browseCommand ?? ( _browseCommand = new BindableCommand<object>(BrowseCommandHandler) ) ; }
        }

        public ICommand ClearCommand
        {
            get { return _clearCommand ?? (_clearCommand = new BindableCommand<object>(ClearCommandHandler)); }
        }


        public string ImagePath 
        {
            get { return _imagePath; }
            set
            {
                _imagePath = value;

                if (!string.IsNullOrEmpty(_imagePath) && !IsValidUri(_imagePath))
                    _imagePath = null;

                NotifyPropertyChange(()=>ImagePath, ()=>ImageUri );
            }
        }

        public Uri ImageUri
        {
            get 
            {
                if (string.IsNullOrEmpty(_imagePath)) 
                    return new Uri(EMPTY_IMAGE_PATH, UriKind.RelativeOrAbsolute);

                Uri uri;
                return Uri.TryCreate(_imagePath, UriKind.RelativeOrAbsolute, out uri)
                           ? uri
                           : new Uri(EMPTY_IMAGE_PATH);
            }
        }

        private void ClearCommandHandler(object state)
        {
            ImagePath = null;
        }

        private void BrowseCommandHandler(object state)
        {
            var ofd = new OpenFileDialog()
                          {
                              CheckFileExists = true,
                              CheckPathExists = true,
                              Multiselect = false,
                              ValidateNames = true,
                              Filter = "Image files|*.png;*.jpg;*.jpeg;*.bmp|All files|*.*"

                          };
            var result = ofd.ShowDialog(this.View as Window);
            if(!result.HasValue || !result.Value) return;

            if(!string.IsNullOrEmpty(ofd.FileName))
                ImagePath = ofd.FileName;
        }

        private static bool IsValidUri(string path)
        {
            Uri uri = null ;
            return Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out uri);
        }
    }
}